package com.building.hcrm.base;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;

import com.building.hcrm.HcrmApplication;
import com.building.hcrm.R;
import com.building.hcrm.R.anim;
import com.building.hcrm.R.id;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.Utils;
import com.building.hcrm.model.CustomerDetail;
import com.building.hcrm.ui.customer.CustomerActivityAudit;
import com.widget.time.JudgeDate;
import com.widget.time.ScreenInfo;
import com.widget.time.WheelMain;

public abstract class BaseActivity extends Activity
{
	protected final String LTAG = this.getClass().getSimpleName();

	private HcrmApplication _hcrmApp;

	protected ImageButton imgbtnHome;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		_hcrmApp = (HcrmApplication) getApplication();
		_hcrmApp.addActivity(this);
	}

	protected HcrmApplication getHcrmApp()
	{
		return _hcrmApp;
	}

	/**
	 * 跳转到新的界面，滑动效果
	 */
	protected void slideNewActivity()
	{
		// 新Activity从右边滑到屏幕上，旧界面从屏幕上滑到左边
		overridePendingTransition(anim.in_from_right, anim.out_to_left);
	}

	protected void slideReturnActivity()
	{
		overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
	}

	protected void setTitle(String titleName)
	{
		TextView titlePageName = (TextView) findViewById(id.titlePageName);
		titlePageName.setText(titleName);
	}

	protected void registerDateClick(final Button btn)
	{
		btn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				try
				{
					if (Utils.isFastDoubleClick())
						return;
					final Button button = (Button) v;

					// 日期設定
					Calendar calendar = Calendar.getInstance();
					int year = calendar.get(Calendar.YEAR);
					int monthOfYear = calendar.get(Calendar.MONTH);
					int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
					if (button.getText().length() > 0)
					{
						year = Integer.parseInt(button.getText().toString().split(ComConst.DATE_SEPARATOR)[0]);
						monthOfYear = Integer.parseInt(button.getText().toString().split(ComConst.DATE_SEPARATOR)[1]) - 1;
						dayOfMonth = Integer.parseInt(button.getText().toString().split(ComConst.DATE_SEPARATOR)[2]);
					}

					// 日付選択ﾀﾞｲｱﾛｸﾞ：OnDateSetListener
					DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener()
					{
						private int _year, _monthOfYear, _dayOfMonth;

						@Override
						public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
						{
							try
							{
								if (_year != year || _monthOfYear != monthOfYear || _dayOfMonth != dayOfMonth)
								{
									_year = year;
									_monthOfYear = monthOfYear;
									_dayOfMonth = dayOfMonth;
									Calendar calendar = Calendar.getInstance();
									calendar.set(year, monthOfYear, dayOfMonth);
									SimpleDateFormat sdf = new SimpleDateFormat(ComConst.DATE_FORMAT, Locale
											.getDefault());
//									Date dtCheck = new Date(sdf.format(calendar.getTime()));
									button.setText(sdf.format(calendar.getTime()));
								}
							}
							catch (Exception e)
							{
								Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
							}
						}
					};

					// 日付選択ﾀﾞｲｱﾛｸﾞ作成＆表示
					DatePickerDialog datePickerDialog = new DatePickerDialog(v.getContext(), dateSetListener, year,
							monthOfYear, dayOfMonth);
					datePickerDialog.show();
				}
				catch (Exception e)
				{
					Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
				}
			}
		});
	}

	protected void registerWheelTimeClick(final Button btn)
	{
		btn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				LayoutInflater inflater = LayoutInflater.from(BaseActivity.this);
				final View timepickerview = inflater.inflate(R.layout.item_time_picker, null);
				ScreenInfo screenInfo = new ScreenInfo(BaseActivity.this);
				final WheelMain wheelMain = new WheelMain(timepickerview);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				wheelMain.screenheight = screenInfo.getHeight();
				String time = btn.getText().toString();
				Calendar calendar = Calendar.getInstance();
				if (JudgeDate.isDate(time, "yyyy-MM-dd"))
				{
					try
					{
						calendar.setTime(dateFormat.parse(time));
					}
					catch (ParseException e)
					{
						e.printStackTrace();
					}
				}
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				wheelMain.initDateTimePicker(year, month, day);
				new AlertDialog.Builder(BaseActivity.this).setTitle("时间选择").setView(timepickerview)
						.setPositiveButton("确定", new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(DialogInterface dialog, int which)
							{
								btn.setText(wheelMain.getTime());
							}
						}).setNegativeButton("取消", new DialogInterface.OnClickListener()
						{
							@Override
							public void onClick(DialogInterface dialog, int which)
							{
							}
						}).show();
			}
		});
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		_hcrmApp.removeActivity(this);
	}
}
